﻿namespace MicroCloud.Identity.Database
{
    /// <summary>
    /// 用户信息数据实体映射配置
    /// </summary>
    public class UserConfiguration : EntityTypeConfigurationBase<User, long>
    {
        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<User> builder)
        {
            builder.HasIndex(m => new { m.UserName, m.DeletedTime }).SetIndex("UserName", true).HasFilter(null);
            builder.HasIndex(m => m.NickName).SetIndex("NickName", false).HasFilter(null);
            builder.HasIndex(m => new { m.UserName, m.Email, m.DeletedTime }).SetIndex("Email", true).HasFilter(null);
            builder.HasIndex(m => new { m.UserName, m.PhoneNumber, m.DeletedTime }).SetIndex("PhoneNumber", true).HasFilter(null);
            switch (DbContextOptions?.DatabaseType)
            {
                case DatabaseType.SqlServer:
                case DatabaseType.PostgreSql:
                case DatabaseType.Oracle:
                    builder.HasIndex(m => new { m.NormalizedUserName, m.DeletedTime }).SetIndex("NormalizedUserName", true).HasFilter(null);
                    builder.HasIndex(m => new { m.UserName, m.NormalizeEmail, m.DeletedTime }).SetIndex("NormalizeEmail", true).HasFilter(null);
                    break;
            }

            ConfigurationAppend(builder);
        }

    }
}
